package com.erp.service.impl;

import com.erp.bean.Order;
import com.erp.dao.OrderDao;
import com.erp.service.OrderService;
import com.erp.util.factroy.ObjectFactory;

import java.sql.SQLException;
import java.util.*;

/**
 * @author lpy
 * 订单业务逻辑实现类
 */
public class OrderServiceImpl implements OrderService {
    OrderDao orderDao = ObjectFactory.getDAOInstance("com.erp.dao.impl.OrderDaoImpl",OrderDao.class);

    @Override
    public boolean add(Order order) {
        boolean flag = false;
        try {
            flag = orderDao.add(order)>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean update(Order order) {
        boolean flag = false;
        try {
            flag = orderDao.update(order)>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean updateBatch(int sid,String... oids) {
        boolean flag = false;
        Set<String> ids = new HashSet<>();
        for (int i=0;i<oids.length;i++){
            ids.add(oids[i]);
        }
        try {
            flag = orderDao.updateBatch(ids,sid)==oids.length;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public Order findById(String oid) {
        Order order = null;
        try {
            order = orderDao.findById(oid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return order;
    }

    @Override
    public List<Order> findAll() {
        List<Order> orders = new ArrayList<>();
        try {
            orders = orderDao.findAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }

    @Override
    public List<Order> findAllSpilt(Integer currentPage, Integer pageSize) {
        List<Order> orders = new ArrayList<>();
        try {
            orders = orderDao.findAllSpilt(currentPage,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }

    @Override
    public List<Order> findAllSpilt(Integer currentPage, Integer pageSize, String column, String keyWords) {
        List<Order> orders = new ArrayList<>();
        try {
            orders = orderDao.findAllSpilt(currentPage,pageSize,column,keyWords);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }

    @Override
    public Long getAllCount() {
        Long num = 0L;
        try {
            num = orderDao.getAllCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }

    @Override
    public Long getAllCount(String colunm, String keyWord) {
        Long num = 0L;
        try {
            num = orderDao.getAllCount(colunm,keyWord);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return num;
    }
}
